package com.coolgor.axnut.web.airuc;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coolgor.airvow.Net.HttpServletRequestUtil;
import com.coolgor.axnut.dto.airuc.HeadlineExecution;
import com.coolgor.axnut.entity.airuc.Headline;
import com.coolgor.axnut.enums.airuc.HeadlineStateEnum;
import com.coolgor.axnut.service.airuc.HeadlineService;


@Controller
@RequestMapping(value = "airuc", method = { RequestMethod.GET, RequestMethod.POST })
public class HeadlineManagementController {
	@Autowired
	private HeadlineService headlineService;
	
	@RequestMapping(value = "/getheadlinebycollegeid", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getHeadlineByCollegeID(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Long collegeID = HttpServletRequestUtil.getLong(request, "collegeid");
		if (collegeID > -1) {
			Headline headlineCondition = new Headline();
			headlineCondition.setCollegeID(collegeID);
			headlineCondition.setStatus(HeadlineStateEnum.ACTIVE.getState());
			try {
				HeadlineExecution exe = headlineService.getHeadlineList(headlineCondition);
				if (exe.getState() == HeadlineStateEnum.SUCCESS.getState()) {
					modelMap.put("HeadlineList", exe.getHeadlineList());
					modelMap.put("success", true);					
				} else {
					modelMap.put("success", false);
					modelMap.put("errMsg", exe.getStateInfo());
				}
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty collegeId");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getheadlinebyid", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getHeadlineByID(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Long lineID = HttpServletRequestUtil.getLong(request, "lineid");
		if (lineID > -1) {
			try {
				Headline headline = headlineService.getHeadline(lineID);
				modelMap.put("headline", headline);
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty lineId");
		}
		return modelMap;
	}
}
